﻿@{
    Layout = "~/_SiteLayout.cshtml";

    Business.Platform.AppLibrary bappLibrary = new Business.Platform.AppLibrary();
    Business.Platform.RoleApp broleApp = new Business.Platform.RoleApp();
    Data.Model.RoleApp roleApp=null;

    string id = Request.QueryString["id"];
    
    Validation.RequireField("Name");
    
    if(IsPost && Validation.IsValid() && id.IsGuid())
    {
        roleApp=broleApp.Get(id.ToGuid());
        if(!Request.Form["Save"].IsNullOrEmpty())
        {
            string name = Request.Form["Name"];
            string type = Request.Form["Type"];
            string appid = Request.Form["AppID"];
            string params1 = Request.Form["Params"];
            string ico = Request.Form["Ico"];

            Data.Model.RoleApp roleApp1 = new Data.Model.RoleApp();

            roleApp1.ID = Guid.NewGuid();
            roleApp1.ParentID = id.ToGuid();
            roleApp1.RoleID = roleApp.RoleID;
            roleApp1.Title = name.Trim();
            roleApp1.ParentID = roleApp.ID;
            roleApp1.Sort = broleApp.GetMaxSort(roleApp.ID);
            roleApp1.Type = 0;
            if(appid.IsGuid())
            {
                roleApp1.AppID = appid.ToGuid();
            }
            else
            {
                roleApp1.AppID = null;
            }
            roleApp1.Params = params1.IsNullOrEmpty() ? null : params1.Trim();
            if(!ico.IsNullOrEmpty())
            {
                roleApp1.Ico = ico; 
            }

            broleApp.Add(roleApp1);
            broleApp.ClearAllDataTableCache();
            Business.Platform.Log.Add("添加了应用模板", roleApp1.Serialize(), Business.Platform.Log.Types.角色应用);
            string refreshID=id;
            <script type="text/javascript">
                alert("添加成功");
                parent.frames[0].reLoad('@refreshID');
            </script>
        }
        
    }

    string appTypesOptions = bappLibrary.GetTypeOptions();
}

<form action="" method="post">
    <br />
    <table cellpadding="0" cellspacing="1" border="0" width="99%" class="formtable">
        <tr>
            <th style="width:80px;">应用名称：</th>
            <td><input type="text" id="Name" name="Name" class="mytext" value="" validate="empty" style="width:75%" /></td>
        </tr>
        <tr>
            <th style="width:80px;">关联程序：</th>
            <td><select id="Type" name="Type" onchange="loadApp(this.value);" style="width:130px;" class="myselect" style="margin-right:5px"><option value=""></option>@Html.Raw(appTypesOptions)</select>
            <select onclick="appidchange(this.value);" style="width:188px;" class="myselect" id="AppID" name="AppID"></select></td>
        </tr>
        <tr>
            <th style="width:80px;">相关参数：</th>
            <td><input type="text" id="Params" name="Params" value="" class="mytext" style="width:75%"/></td>
        </tr>
        <tr>
            <th style="width:80px;">图标：</th>
            <td><input type="text" name="Ico" id="Ico" class="myico" source="/Images/ico" value="" style="width: 75%"/></td>
        </tr>
    </table>
    <div class="buttondiv">
        <input type="submit" value="保存" class="mybutton" name="Save" onclick="return new RoadUI.Validate().validateForm(document.forms[0]);" />
        <input type="button" value="返回" class="mybutton" onclick="window.location='Body'+'@Request.Url.Query';" />
    </div>
    </form>
    <script type="text/javascript">
        $(function ()
        {
            loadApp($("#Type").val());
        });
        function appidchange(value)
        {
            var options = $("#AppID option");
            for (var i = 0; i < options.length; i++)
            {
                if (value && options.eq(i).val() == value)
                {
                    $("#Name").val(options.eq(i).text());
                }
            }
        }
        function loadApp(value)
        {
            if (!value)
            {
                return false;
            }
            $.ajax({ url: "GetApps", type: "post", data: { "type": value }, dataType: "text", async: false, cache: false, success: function (txt)
            {
                var $appid = $("#AppID");
                $("option", $appid).remove();
                $appid.append('<option value=""></option>'+txt);
            }
            });
        }
    </script>